package com.netease;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @projectName: modeling-dev
 * @package: com.netease
 * @className: ExcelDemo
 * @author: aochong
 * @description: TODO
 * @date: 2023/4/12 14:58
 * @version: 1.0
 */
public class ExcelDemo {
    public  static  void  main(String[]  args)  {
        HSSFWorkbook workbook  =  new  HSSFWorkbook();
        HSSFSheet sheet  =  workbook.createSheet("sheet1");

        //  设置第1行1~2列的背景色为绿色
        for  (int  column  =  0;  column  <  2;  column++)  {
            HSSFCell cell = sheet.createRow(0).getCell(column);
            if  (cell  ==  null)  {
                cell  =  sheet.createRow(0).createCell(column);
            }
            HSSFCellStyle style  =  workbook.createCellStyle();
            style.setFillForegroundColor(HSSFColor.GREEN.index);
            style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            cell.setCellStyle(style);
        }

        //  设置第1行3~5列的背景色为蓝色
        for  (int  column  =  2;  column  <  5;  column++)  {
            HSSFCell  cell  =  sheet.createRow(0).getCell(column);
            if  (cell  ==  null)  {
                cell  =  sheet.createRow(0).createCell(column);
            }
            HSSFCellStyle  style  =  workbook.createCellStyle();
            style.setFillForegroundColor(HSSFColor.BLUE.index);
            style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            cell.setCellStyle(style);
        }

        //  写入到文件中
        try  {
            FileOutputStream  fileOut = new FileOutputStream("workbook.xls");
            workbook.write(fileOut);
            fileOut.close();
        }  catch  (IOException  e)  {
            e.printStackTrace();
        }
    }
}
